package com.gun.management.module.raffle.service.lotteryrecord;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gun.management.framework.mybatis.core.util.MyBatisUtils;
import com.gun.management.framework.web.core.util.WebFrameworkUtils;
import com.gun.management.module.raffle.controller.admin.lotterylog.vo.LotteryLogPageReqVO;
import com.gun.management.module.raffle.controller.admin.lotterylog.vo.LotteryLogRespVO;
import com.gun.management.module.raffle.enums.ErrorCodeConstants;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import com.gun.management.module.raffle.controller.admin.lotteryrecord.vo.*;
import com.gun.management.module.raffle.dal.dataobject.lotteryrecord.LotteryRecordDO;
import com.gun.management.framework.common.pojo.PageResult;
import com.gun.management.framework.common.pojo.PageParam;
import com.gun.management.framework.common.util.object.BeanUtils;

import com.gun.management.module.raffle.dal.mysql.lotteryrecord.LotteryRecordMapper;

import static com.gun.management.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.gun.management.module.raffle.enums.ErrorCodeConstants.*;

/**
 * 用户抽奖履历 Service 实现类
 *
 * @author 测试账号
 */
@Service
@Validated
public class LotteryRecordServiceImpl implements LotteryRecordService {

    @Resource
    private LotteryRecordMapper lotteryRecordMapper;

    @Override
    public Long createLotteryRecord(LotteryRecordSaveReqVO createReqVO) {
        // 插入
        LotteryRecordDO lotteryRecord = BeanUtils.toBean(createReqVO, LotteryRecordDO.class);

        Long userId = WebFrameworkUtils.getLoginUserId();
        lotteryRecord.setCreator(String.valueOf(userId));
        lotteryRecord.setCreateTime(LocalDateTime.now());
        lotteryRecordMapper.insert(lotteryRecord);
        // 返回
        return lotteryRecord.getId();
    }

    @Override
    public void updateLotteryRecord(LotteryRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateLotteryRecordExists(updateReqVO.getId());
        // 更新
        LotteryRecordDO updateObj = BeanUtils.toBean(updateReqVO, LotteryRecordDO.class);

        Long userId = WebFrameworkUtils.getLoginUserId();
        updateObj.setUpdater(String.valueOf(userId));
        updateObj.setUpdateTime(LocalDateTime.now());
        lotteryRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteLotteryRecord(Long id) {
        // 校验存在
        validateLotteryRecordExists(id);
        // 删除
        lotteryRecordMapper.deleteById(id);
    }

    private void validateLotteryRecordExists(Long id) {
        if (lotteryRecordMapper.selectById(id) == null) {
            throw exception(LOTTERY_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public LotteryRecordDO getLotteryRecord(Long id) {
        return lotteryRecordMapper.selectById(id);
    }

    /**
     * 获得用户抽奖履历分页
     * @param userId
     * @param telantId
     * @param reqVO
     * @return
     */
    @Override
    public PageResult<LotteryRecordRespVO> getLotteryRecordPage(Long userId, Long telantId, LotteryRecordReqVO reqVO) {
        IPage<LotteryRecordRespVO> pageResult =  lotteryRecordMapper.selectLotteryRecordPageList( MyBatisUtils.buildPage(reqVO),
                reqVO.getLotteryCategoryId()  ,reqVO.getPrizeCategoryId(), reqVO.getStatus()  ,reqVO.getLotteryTime(),
                reqVO.getLotteryName(),reqVO.getPrizeName(),reqVO.getNickName());

        return   new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
    }

}